package org.langcom;

import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.apache.commons.lang.ClassUtils;
import org.apache.commons.lang.StringUtils;


public class ResouceUtils {
	
	private static final String PROPERTY_LABEL_POSTFIX = "label";

	public static String getMessage(Class sourceClass,String descriptor,String key, String defaultValue) {
		if (StringUtils.isEmpty(key)) {
			key = sourceClass.getSimpleName() + "." + descriptor + "." + PROPERTY_LABEL_POSTFIX;
		}
		String value = defaultValue;
		String packageName = ClassUtils.getPackageName(sourceClass);
		if (packageName != null) {
			try {
				value = ResourceBundle.getBundle(
					packageName + ".messages",
					Locale.getDefault(),
					sourceClass.getClassLoader()).getString(key);
			} catch (MissingResourceException e) {
				System.out.println(key + " " + e.getMessage());
			}
		}
		return value;
	}
}
